package com.xiansi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiansi.mapper.GameMapper;
import com.xiansi.mapper.TeamMapper;
import com.xiansi.pojo.Game;
import com.xiansi.pojo.GameExample;
import com.xiansi.pojo.Team;
import com.xiansi.pojo.TeamExample;
import com.xiansi.vo.QueryGameVO;
import com.xiansi.vo.QueryTeamVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 赛程管理的业务逻辑层
 */
@Service
public class GameService {
    @Resource
    private GameMapper gameMapper;
    @Resource
    private TeamMapper teamMapper;

    /**
     * 查询所有赛程
     *
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Game> queryAll() {

        List<Game> games = gameMapper.selectByExample(null);
        for (Game game : games) {
            game.setHomeTeam(teamMapper.selectByPrimaryKey(game.getHomeTeamId()));//根据主队id查询到的主队信息
            game.setVisitingTeam(teamMapper.selectByPrimaryKey(game.getVisitingTeamId()));
        }
        return games;
    }

    /**
     * 分页查询赛程
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Game> queryByPage(Integer pageNum, Integer pageSize, QueryGameVO vo) {
        //多条件
        GameExample gameExample = new GameExample();
        TeamExample teamExample = new TeamExample();
        //创建条件的容器
        GameExample.Criteria gameExampleCriteria = gameExample.createCriteria();
        TeamExample.Criteria teamExampleCriteria = teamExample.createCriteria();

        if (vo != null) {
            if (vo.getChineseName() != null && !"".equals(vo.getChineseName().trim())) {
                teamExampleCriteria.andChineseNameLike("%" + vo.getChineseName() + "%");
                List<Team> list = teamMapper.selectByExample(teamExample);
                List<Integer> list1 = new ArrayList<>();
                for (Team team : list) {
                    Integer teamId = team.getTeamId();
                    list1.add(teamId);
                }
                //根据球队id模糊or查询
                gameExampleCriteria.andHomeTeamIdIn(list1);
                gameExample.or().andVisitingTeamIdIn(list1);
            }
            if (vo.getBeginDate() != null) {
                if (vo.getEndDate() == null) {
                    vo.setEndDate(new Date());
                }
                gameExampleCriteria.andGameDateBetween(vo.getBeginDate(), vo.getEndDate());
            }
            if (vo.getTypeId() != null && vo.getTypeId() != -1) {
                gameExampleCriteria.andTypeIdEqualTo(vo.getTypeId());
            }

        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<Game> list = gameMapper.selectByExample(gameExample);
        for (Game game : list) {
            game.setHomeTeam(teamMapper.selectByPrimaryKey(game.getHomeTeamId()));//根据主队id查询到的主队信息
            game.setVisitingTeam(teamMapper.selectByPrimaryKey(game.getVisitingTeamId()));
        }
        return new PageInfo<>(list);
    }

    /**
     * 新增比赛
     *
     * @param game
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer addGame(Game game) {
        game.setIsDel(0);
        return gameMapper.insertSelective(game);
    }

    /**
     * 根据id查询赛程
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Game queryById(Integer id) {
        return gameMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据主键id更新数据
     *
     * @param game
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer updateGame(Game game) {
        return gameMapper.updateByPrimaryKeySelective(game);
    }

    /**
     * 根据id删除赛程
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer deleteGame(Integer id) {
        return gameMapper.deleteByPrimaryKey(id);
    }

}
